package com.example.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.customview.ARGBView;
import com.example.studyapplication.R;
import com.example.util.FastBlur;
import com.example.util.RSBlur;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * @Description:绘制页面
 * @Author: 葛浩杰
 * @Version: 1.0 2020/12/21
 */
public class DrawActvity extends Activity {

    private ARGBView mArgbView;
    private ImageView mIvTest;
    private ImageView iv_top;

    public static void startActivity(Context context, int position){
        Intent intent = new Intent(context,DrawActvity.class);
        intent.putExtra("type", position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw);

        initView();
        initData();
    }

    private void initView() {

        mArgbView = findViewById(R.id.custom_view);
        mIvTest = findViewById(R.id.iv_test);

        Bitmap output = Bitmap.createBitmap(800, 210, Bitmap.Config.ARGB_8888);
        output.eraseColor(Color.parseColor("#F38390"));


//        Bitmap blurBitmap = FastBlur.blur(BitmapFactory.decodeResource(getResources(), R.mipmap.blurbg),10,false);
//        Bitmap blurBitmap = RSBlur.blur(this,drawMyBitmap2(output, 80),10);
//        Drawable drawable = new BitmapDrawable(blurBitmap);
//        Drawable drawable = new BitmapDrawable(drawMyBitmap2(output, 20));
//        mArgbView.setBackground(drawable);
//        mIvTest.setImageBitmap(blurBitmap);
        mIvTest.setImageBitmap(drawMyBitmap2(output, 20));
//        iv_top.setImageBitmap(blurBitmap);
    }

    private void initData() {

        int type = getIntent().getIntExtra("type",0);
        mArgbView.setType(type);

    }


    private Bitmap drawMyBitmap2(Bitmap bitmap, int roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

//        final Rect block = new Rect(0, 0, 20, 20);
//        canvas.drawRect(block, paint);
//        final Rect block2 = new Rect(0, 100 - 20, 20, 100);
//        canvas.drawRect(block2, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setAlpha(150);
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

}
